package com.ffcc.fitness.websocket.config;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;
import java.util.Map;

/**
 * WebSocket Token 配置器
 * 用于在WebSocket握手时提取查询参数
 */
public class WebSocketTokenConfigurator extends ServerEndpointConfig.Configurator {
    
    @Override
    public void modifyHandshake(ServerEndpointConfig config, 
                              HandshakeRequest request, 
                              HandshakeResponse response) {
        
        // 获取查询参数
        String queryString = request.getQueryString();
        if (queryString != null) {
            config.getUserProperties().put("queryString", queryString);
        }
        
        // 也可以通过参数Map获取
        Map<String, List<String>> parameters = request.getParameterMap();
        List<String> tokenList = parameters.get("token");
        if (tokenList != null && !tokenList.isEmpty()) {
            config.getUserProperties().put("token", tokenList.get(0));
        }
        
        super.modifyHandshake(config, request, response);
    }
}
